cmake_minimum_required(VERSION 3.5.0)

project(cubedsphere LANGUAGES Fortran)

#Compiler and Machine settings
if (CMAKE_Fortran_COMPILER_ID MATCHES Intel)
  if (CMAKE_BUILD_TYPE MATCHES DEBUG)
    set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -cpp -g -qopenmp -r8 -assume byterecl -traceback -fp-stack-check -no-wrap-margin -check bounds -check format -check uninit -check output_conversion")
    #set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -cpp -g -qopenmp -r4 -assume byterecl -traceback -fp-stack-check -no-wrap-margin -check bounds -check format -check uninit -check output_conversion")
  else ()
    set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -cpp -r8 -O3 -parallel -qopenmp -assume byterecl -mkl -no-wrap-margin")
    #set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -cpp -r4 -O3 -parallel -qopenmp -assume byterecl -qmkl -no-wrap-margin")
  endif ()
elseif (CMAKE_Fortran_COMPILER_ID MATCHES GNU)
  if (CMAKE_BUILD_TYPE MATCHES DEBUG)
    set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -g -cpp -gdwarf -Og -fbacktrace -fcheck=all -fdefault-real-8 -ffree-line-length-none -fdollar-ok -fopenmp -lgomp")
    #set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -g -cpp -gdwarf -Og -fbacktrace -fcheck=all -ffree-line-length-none -fdollar-ok -fopenmp -lgomp")
  else ()
    #set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -cpp -P -traditional -O3 -fopenmp -lgomp -fdefault-real-8 -ffree-line-length-none -fdollar-ok")
    set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -cpp -P -traditional -O3 -fopenmp -lgomp -ffree-line-length-none -fdollar-ok")
  endif ()
endif ()

# Link netCDF library
if (DEFINED ENV{NETCDF} AND (NOT DEFINED ENV{NETCDF_ROOT}))
  set(ENV{NETCDF_ROOT} $ENV{NETCDF})
endif ()
if (DEFINED ENV{NETCDF_ROOT})
  include_directories("$ENV{NETCDF_ROOT}/include")
  link_directories("$ENV{NETCDF_ROOT}/lib")
else ()
  find_package(PkgConfig)
  if (PKG_CONFIG_FOUND)
    pkg_search_module(NETCDF REQUIRED netcdf)
    if (NETCDF_FOUND)
      include_directories(${NETCDF_INCLUDEDIR})
      link_directories(${NETCDF_LIBDIR})
    else ()
      message(FATAL_ERROR "Pkg-config could not find netcdf library!")
    endif ()
  else ()
    message(FATAL_ERROR "Unable to find pkg-config library!")
  endif ()
endif ()
set(EXTERNAL_LIBS netcdff netcdf)

set(KeSpectra
  src/constants_mod.F90
  src/namelist_mod.F90
  src/IO_mod.F90
  src/variables_mod.F90
  src/calculation_mod.F90
)
add_library(KeSpectra ${KeSpectra})
target_link_libraries(KeSpectra ${EXTERNAL_LIBS})

add_executable(main.exe src/main.F90)
target_link_libraries(main.exe KeSpectra)

